package com.lizanyi.teaFriend.controller;

import com.lizanyi.teaFriend.pojo.TeaAdmin;
import com.lizanyi.teaFriend.service.AdminService;
import com.lizanyi.teaFriend.tools.Constans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/")
    public String index(){
        return "index";
    }
    @RequestMapping(value = "/dologin",method=RequestMethod.POST)
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        ModelMap modelMap,
                        HttpSession session) throws Exception{

        TeaAdmin teaAdmin=adminService.login(account,password);
        if(null!=teaAdmin){
            //登录成功
            //放入session
            session.setAttribute(Constans.ADMIN_SESSION,teaAdmin);
            System.out.println(teaAdmin);
            return "redirect:/to/main";
        }else {
            modelMap.addAttribute("error", "用户名或密码不正确");
            return "index";
        }

    }





}
